<?php

ini_set("display_errors", "On");
error_reporting(E_ALL);

// 引入数据库连接文件
require_once 'coon/connection.php';

$username = $_POST['username'];
$pwd =  $_POST['pwd'];
// $addStuPwd = password_hash("123456", PASSWORD_DEFAULT);

// 查询语句
$querySql = "select id,pwd,username from user where username='$username'";
// 连接数据库，并查询
$result = mysqli_query($GLOBALS['conn'], $querySql);

//echo $result_config;
if (mysqli_num_rows($result) <= 0) {
	echo "
<script>
    alert('该用户暂无注册！');
    history.go(-1);
</script>";
} else {
	$now = date('Y-m-d H:i:s', time());
	while ($row = mysqli_fetch_array($result)){
		if (password_verify($pwd, $row['pwd'])) {
			// 设置cookie来区分用户登录状态
			setcookie("userName",$row['username']);
			setcookie("userID",$row['id']);
			echo "
            <script>
                alert('登录成功！');
                window.location.href='index.php';
            </script>
        ";
		} else {
			echo "
            <script>
                alert('登录失败，用户名或密码错误！');
                history.back();
            </script>
        ";
		}
	}
	
	
}

